<template>
  <div>
    <van-nav-bar fixed placeholder title="黑马头条 - 登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="loginForm.mobile"
        label="手机号"
        placeholder="请输入手机号"
        required
        :rules="[
          {
            required: true,
            pattern: /^1[34578]\d{9}$/,
            message: '请输入正确的手机号',
          },
        ]"
      />
      <van-field
        v-model="loginForm.code"
        type="password"
        label="验证码"
        placeholder="请输入验证码"
        required
        :rules="[
          { required: true, pattern: /^\d{6}$/, message: '请输入6位验证码' },
        ]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          :disabled="islogin"
          :loading="islogin"
          loading-text="正在玩命加载中..."
          native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { loginApi } from '@/api'
import { setToken } from '@/utils/token.js'
export default {
  data () {
    return {
      loginForm: {
        mobile: '13888888678',
        code: '246810'
      },
      islogin: false
    }
  },
  methods: {
    async onSubmit () {
      this.islogin = true
      try {
        const res = await loginApi({ ...this.loginForm })
        setToken(res.data.data.token)
        localStorage.setItem('refresh', res.data.data.refresh_token)
        this.$notify({ type: 'success', message: '恭喜你,登陆成功' })
        // 登录获取原路径,无原路径则,到登录界面
        this.$router.replace({ path: this.$route.query.comBack || '/layout' })
        this.islogin = false
      } catch (err) {
        this.islogin = false
      }
    }
  }

}
</script>

<style>
</style>
